

#region Using directives

using System;
using Microsoft.WindowsMobile.PocketOutlook;
using System.IO;
using System.Xml.Serialization;
using System.Xml;
using System.Text;

#endregion
/** 
COPYRIGHT (C) 2008 Piyusha Desai. All Rights Reserved.
* Read Licence.txt before using this application.
*/
namespace SmsBuddy
{
    
    public class Buddy: IDisposable
    {
        // outlook session
        static OutlookSession outlookSession = new OutlookSession();

        public OutlookSession OutlookSession
        {
            get { return outlookSession; }
        }

        //group object
        GroupCollection groups = new GroupCollection();
       
        public GroupCollection Groups
        {
            get { return groups; }
        }

       

           static readonly string dataFile = "Buddy.xml";

    
   
        public static string ApplicationDataDirectory
        {
            get
            {
                return System.Environment.GetFolderPath(
                    Environment.SpecialFolder.ApplicationData) + "\\Buddy";
            }
        }

      
      
       
        public static string DataFile
        {
            get 
            {
                return ApplicationDataDirectory + "\\" + dataFile;
            }
        }

        static readonly string contactPropertyName = "BuddyGroup";
       
      
       
        public static string ContactPropertyName
        {
            get { return contactPropertyName; }
        }
        
        static readonly string missedCallIdName = "Microsoft.Buddy.MissedCall";
     
        
        public static string MissedCallIdName
        {
            get { return missedCallIdName; }
        }

        static readonly string remoteAdminIdName = "Microsoft.Buddy.RemoteAdmin";
        public static string RemoteAdminIdName
        {
            get { return remoteAdminIdName; }
        }

        static readonly string textForwardingName = "Microsoft.Buddy.TextForwarding";
        
      
        public static string TextForwardingName
        {
            get { return Buddy.textForwardingName; }
        }
              

            
        public Group DefaultGroup
        {
            get { return groups[0]; }
        }

      
     
   
        public Group UnknownGroup
        {
            get { return groups[1]; }
        }

        SmsOptionCode textMessageOptions = new SmsOptionCode();

      
        public SmsOptionCode TextMessageOptions
        {
            get { return textMessageOptions; }
            set { textMessageOptions = value; }
        }

        public Buddy()
        {
            CheckCustomProperty();
        }
            
   
      
        public void CheckCustomProperty()
        {

            if (outlookSession.Contacts.Items.Count > 0)
            {
                Contact contact = outlookSession.Contacts.Items[0];

                #region Custom Property
                // Make sure that our custom property is added to the contacts.  

                // TODO: Technically someone could add their first user while the app is running and then 
                // we will fail because we didn't add the property.  So, i should change this, but i don't want
                // to just add everytime.
                contact.Properties.Add(Buddy.ContactPropertyName, typeof(int), PimPropertyFlags.DoNotReplicate | PimPropertyFlags.Indexed);
                #endregion
            }
        }

 
         public void Save()
        {
            if (!Directory.Exists(ApplicationDataDirectory))
            {
                Directory.CreateDirectory(ApplicationDataDirectory);
            }

            StreamWriter sw = new StreamWriter(DataFile);
            XmlSerializer xml = new XmlSerializer(typeof(Buddy));
            xml.Serialize(sw, this);
            sw.Close();
        }

    
        // Load the state of the Buddy from the data file
     
        public static Buddy Load()
        {
            if (!Directory.Exists(ApplicationDataDirectory))
            {
                Directory.CreateDirectory(ApplicationDataDirectory);
            }

            Buddy ms = null;
            try
            {
                StreamReader sr = new StreamReader(DataFile);
                XmlSerializer xs = new XmlSerializer(typeof(Buddy));
                ms = (Buddy)xs.Deserialize(sr);
                sr.Close();
            }
            catch (IOException)
            {
                ms = new Buddy();
            }

            return ms;
        }
                
    
        //check for any enable group
        public bool AreAnyEnabled()
        {
            bool enabled = false;
            // check for group is enable or not.
            foreach (Group group in Groups)
            {
                enabled = group.Enabled;
                if (enabled)
                {                    
                    break;
                }
            }
            return enabled;
        }


        #region IDisposable Members

        public void Dispose()
        {
            if (outlookSession != null)
            {
                outlookSession.Dispose();
                outlookSession = null;
            }
        }

        #endregion
    }
}
